/* 
 * Copyright (C) 2010 Christian Schneider
 * 
 * This file is part of Androfire.
 * 
 * Androfire is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Androfire is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Androfire.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */




package com.tritop.androfire;

import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.graphics.Paint.Style; 
import java.lang.Math;
import java.lang.Double;
import java.lang.Float;
import java.lang.Integer;


import android.view.MotionEvent;

public class Androfire extends Activity {
    /** Called when the activity is first created. */
	   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new Panel(this));

    }
    
    
    class Panel extends SurfaceView implements SurfaceHolder.Callback {
        private updateThread _thread;
        protected int mwidth,mheight;
        protected int lastTouchX,lastTouchY,origX,origY;
        protected Paint txtPaint;
        protected int punkt[];
        protected Paint pinselpal[];
        protected int BURNFACTOR;
        protected int DELTA;
        int tmp,x,y,i;
        public Panel(Context context) {
            super(context);
            mwidth = this.getWidth();
            mheight = this.getHeight();
            pinselpal = new Paint[256];
            txtPaint=new Paint();
            txtPaint.setARGB(250, 200, 200, 200);
            punkt = new int[80*80];
            BURNFACTOR = 75;
            DELTA = 4;
            for(int i=0;i<80*80;i++)punkt[i]=0;
            createPalette();
            calcHotspots();
            getHolder().addCallback(this);
           
            _thread = new updateThread(getHolder(), this);
        }
        
        protected void createPalette(){
        	int tmpar[][]= new int[256][3];
        	double PI = 3.1414926;
        	    for(int i=0;i<160;i++)
        		 tmpar[i][0]=new Double(255*Math.sin((i*PI)/160)).intValue();
        		for(int h=64;h<224;h++)
        		 tmpar[h][1]=new Double(255*Math.sin((h-64)*PI/160)).intValue();
        		for(int k=127;k<255;k++)
        		 tmpar[k][2]= new Double(255*Math.sin((k-127)*PI/150)).intValue();
        		for(int u=0;u<15;u++)
        		 tmpar[u][2]= new Double(255*Math.sin((255+u-127)*PI/150) ).intValue();
           for(int i=0;i<=255;i++){
        	   pinselpal[i]= new Paint();
        	   pinselpal[i].setARGB(255, tmpar[i][0],tmpar[i][1],tmpar[i][2]);
        	   pinselpal[i].setAntiAlias(false);
        	   pinselpal[i].setDither(false);
       
           }
           pinselpal[0].setARGB(255, 0,0,0);
        }
        
        public void calcHotspots(){
        	for( i=0; i< 80*2;i++){
        		if(Math.random()*100 <= BURNFACTOR){
        			punkt[i]=240;
        		}
        		else{ punkt[i]=0;}
        	}
        }
        
        public void calcIntensity(){
        	
        	for( i=80*80-81;i>81;i--){
        		tmp=(punkt[i-1]+punkt[i+1]+punkt[i-80]+punkt[i-80]+punkt[i-80])/5;
        		if(tmp<DELTA)tmp=DELTA;
        		punkt[i+80]=tmp-DELTA;
        	}
        }
        @Override
        public void onDraw(Canvas canvas) {
        	canvas.drawColor(Color.BLACK);
           for( y=0;y<320;y+=4){
        	for( x=0;x<320;x+=4){
        		canvas.drawRect(x, mheight-y, x+4, mheight-y+4,pinselpal[punkt[y/4*80+x/4]]);

            }
           }
           //canvas.drawRect(10, 10,300, 70,pinselpal[0]);
           //canvas.drawText("X:"+origX+" Y:"+origY, 10, 30, txtPaint);
           
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent evnt){
          if(evnt.getAction()== MotionEvent.ACTION_MOVE){
            lastTouchX=(int)evnt.getX();
            lastTouchY=(int)evnt.getY();
            origX=lastTouchX;
            origY=lastTouchY;
            lastTouchY=mheight-lastTouchY;
            if(lastTouchY>300)lastTouchY=300;
            punkt[lastTouchY/4*80+lastTouchX/4]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+1]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+2]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+3]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+80]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+81]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+82]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+83]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+160]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+161]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+162]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+163]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+240]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+241]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+242]=0;
            punkt[lastTouchY/4*80+lastTouchX/4+243]=0;
          }
	     return true;
        }
        
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Auto-generated method stub
        	//mwidth = this.getWidth();
            //mheight = this.getHeight();
        }
 
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
        	mwidth = this.getWidth();
            mheight = this.getHeight();
            _thread.setRunning(true);
            _thread.start();

        }
 
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                   
                }
            }
        }
    }

    
    
    
    class updateThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private Panel _panel;
        private boolean _run = false;
 
        public updateThread(SurfaceHolder surfaceHolder, Panel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }
 
        public void setRunning(boolean run) {
            _run = run;
        }
 
        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                _panel.calcHotspots();
            	_panel.calcIntensity();
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                    	
                        _panel.onDraw(c);
                    }
                } finally {
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }

    
    
    
}